package com.ruige.service.listenner;

import com.ruige.service.service.RuigeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * 监听机制,服务启动后自动开始发送心跳
 */
@Component
public class MyListener implements ApplicationListener<ApplicationStartedEvent> {

    @Autowired
    private RuigeService ruigeService;

    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        //每6秒发送一个心跳
                        Thread.sleep(8000);
                        ruigeService.ping();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }finally {
                        continue;
                    }
                }
            }
        }).start();

        }
    }

